<?php

namespace App\Http\Controllers;

use App\ApiResponse;
use App\Http\Resources\ArchiveReportResource;
use App\Http\Resources\ArchiveResource;
use App\Models\Archive;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;

class ArchivesController extends Controller
{

    use ApiResponse;

    //

    public function query()
    {
        $http = new Client();
        $list = Archive::query()->orderBy('created_at', 'desc')->where("status", '<', 4)->get();
        $url = env("QUERY_URL");
        $ids = [];
        foreach ($list as $item) {
            $ids[] = $item->record_no;
        }
        if($ids){
            $response = $http->get($url, ['query' => ['ids' => implode(",", $ids)]]);
            $result = json_decode((string)$response->getBody(), true);
            if ($result['status'] == 1) {
                $tasklist = $result['data'];

            }
        }

    }

    public function report(Request $request)
    {

        return response()->json(['msg' => '获取成功', 'status' => 1, 'data' => ArchiveReportResource::collection(Archive::all())]);
    }

    public function index(Request $request)
    {
        $archives = Archive::whereUserId($request->user('api')->id)->paginate();

        return $this->api("获取成功", 200, ArchiveResource::collection($archives));
    }


    public function upload(Request $request)
    {


        $insert = [];
        $date = date("Y-m-d");

        $public_videos_path = "videos/" . $date;
        $public_images_path = "images/" . $date;
        $public_audios_path = "audios/" . $date;

        /**
         * 查看是否有视频
         */
//        dd($public_video_path);
        if ($request->hasFile("video")) {
            $video_file = $request->file("video");
            if (!$video_file->isValid()) {
                return $this->api("视频上传失败", 400);
            }
            $allowed_extensions = ["mp4", "avi", "3gp"];
            if ($video_file->getClientOriginalExtension() && !in_array($video_file->getClientOriginalExtension(), $allowed_extensions)) {
                return $this->api("视频格式错误，只能上传mp4，avi，3gp的视频", 400);
            }

            $video_path = \Storage::disk("public")->putFile($public_videos_path, $video_file);

            $video_thumb_file = $request->file("video_thumb");
            $allowed_extensions = ["png", "jpg", "gif"];

            if ($video_thumb_file->getClientOriginalExtension() && !in_array($video_thumb_file->getClientOriginalExtension(), $allowed_extensions)) {
                return $this->api("您只能上传PNG、JPG或GIF格式的图片！", 400);
            }
            $video_thumb_path = \Storage::disk("public")->putFile($public_images_path, $video_thumb_file);
            $insert['video'] = $video_path;
            $insert['video_thumb'] = $video_thumb_path;
        }
        if ($request->hasFile("audio")) {
            $audio_file = $request->file("audio");
            $allowed_extensions = ["amr", "acc", "mp3"];
            if ($video_file->getClientOriginalExtension() && !in_array($video_file->getClientOriginalExtension(), $allowed_extensions)) {
                return $this->api("视频格式错误，只能上传amr，mp3，acc的语音", 400);
            }
            $audio_path = \Storage::disk("public")->putFile($public_audios_path, $audio_file);
            $insert['audio'] = $audio_path;
        }

//        dd($request->files);
        if ($request->hasFile("photos")) {
            $photos = [];
            $photo_files = $request->file("photos");
//            dd($photo_files);
            foreach ($photo_files as $key => $photo_file) {
                if (!$photo_file->isValid()) {
                    return $this->api("图片上传失败", 400);
                }
                if (!empty($photo_file)) {//此处防止没有多文件上传的情况
                    $allowed_extensions = ["png", "jpg", "gif"];
                    if ($photo_file->getClientOriginalExtension() && !in_array($photo_file->getClientOriginalExtension(), $allowed_extensions)) {
                        return $this->api("您只能上传PNG、JPG或GIF格式的图片！", 400);
                    }
                    $photo_path = \Storage::disk("public")->putFile($public_images_path, $photo_file);
//                    $photo_path = $photo_file->storePublicly($public_images_path);

                    $photos[] = $photo_path;
                }
            }
            $insert['photos'] = $photos;
        }

        $insert['record_no'] = $request->input('record_no');
        $insert['latitude'] = $request->input('latitude');
        $insert['longitude'] = $request->input('longitude');
        $insert['message'] = $request->input('message');
        $insert['record_num'] = $request->input('record_num');
        $insert['record_people'] = $request->input('record_people');
        $insert['record_username'] = $request->input('record_username');
        $insert['address'] = $request->input('address');
        $insert['record_date'] = Carbon::createFromTimestamp($request->input('record_date'));
        $user = $request->user('api');
        $insert['user_id'] = $user->id;
        $insert['phone'] = $user->phone;
        $insert['process'] = [];
//        dd($insert);
        $archive = Archive::create($insert);
        if ($archive) {
            return $this->api("上传成功");
        } else {
            return $this->api("上传失败");
        }

    }
}
